package com.dshore.messagecenter.service.warning.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dshore.messagecenter.dao.warning.ExceptionMessageDao;
import com.dshore.messagecenter.model.basedata.MdApp;
import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.model.topicqueue.MdTopic;
import com.dshore.messagecenter.model.warning.MrExceptionMessage;
import com.dshore.messagecenter.service.warning.ExceptionMessageService;
import com.dshore.messagecenter.util.Page;
import com.dshore.messagecenter.util.RabbitUtil;
/**
 * 异常信息服务层
 * @author Administrator
 *
 */
@Service
public class ExceptionMessageSeviceImpl implements ExceptionMessageService{
	@Autowired 
	private ExceptionMessageDao exceptionMessageDao;
	
	@Autowired 
	private RabbitUtil rabbitUtil;
	
	/**
	 * 获取表格值
	 */
	public Page<MrExceptionMessage> queryListAll(MrExceptionMessage exceptionMessage) {
		// 总记录数
				long total = exceptionMessageDao.queryCountByPrama(exceptionMessage);
				if(total<=exceptionMessage.getLimit()||total<exceptionMessage.getOffset()) {
					exceptionMessage.setOffset(0);
				}
				// 模糊查询
				List<MrExceptionMessage> list = exceptionMessageDao.queryListByPrama(exceptionMessage);
				// 重构对象,方便扩展
					for (MrExceptionMessage exceptionMessage1 : list) {
						Map<String,Object> ws = exceptionMessageDao.selectFieldName(exceptionMessage1);						
						if(ws!=null) {
							exceptionMessage1.setTopicQueueName((String)ws.get("Topic_Queue_Name"));
							exceptionMessage1.setAppName((String)ws.get("App_Name"));
							exceptionMessage1.setNumber(Integer.parseInt(ws.get("number").toString()));
							exceptionMessage1.setFinalTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)ws.get("latestTime")));
							exceptionMessage1.setUrl((String)ws.get("url"));
							exceptionMessage1.setStatus((String)ws.get("status"));
						}else {
							continue; 					
						}
					}
					
					Page<MrExceptionMessage> page = new Page<MrExceptionMessage>(exceptionMessage.getLimit(), total, exceptionMessage.getOffset(),list);
					return page;
	}
	
	/**
	 * 获取主题队列名称下拉
	 */
	@Override
	public List<MdTopic> getTopicName() {
		return exceptionMessageDao.getTopicName();
	}
	/**
	 * 获取应用名称下拉
	 */
	@Override
	public List<MdApp> getAppName() {
		return exceptionMessageDao.getAppName();
	}
	/**
	 * 删除
	 */
	@Override
	public ResponseResult<Void> deleteByCodes(String apps,String topics) {
		ResponseResult<Void> responseResult = new ResponseResult<Void>(200,"删除成功");
		String[] app =apps.split(",");
		String[] topic=topics.split(",");
	    exceptionMessageDao.deleteByCodes(app,topic);
		return responseResult;
	}
	
	/**
	 * 异常信息添加
	 * @param list
	 * @return
	 */
	public String addException(List<MrExceptionMessage> exceptionMessages) {
		int[] i=exceptionMessageDao.addException(exceptionMessages);
		return "插入"+i.length+"条成功";
		
	}
	/**
	 * 重发
	 */
	@Override
	public ResponseResult<Void> resend(String appCode, String topicQueueNo) {
		ResponseResult<Void> responseResult = new ResponseResult<Void>();
		boolean bool = rabbitUtil.msgResend(topicQueueNo,appCode);
		if(bool) {
			responseResult.setState(200);
			responseResult.setMessage("已经重发");
		}else {
			responseResult.setState(400);
			responseResult.setMessage("重发失败");
		}
		return responseResult;
	}

}
